﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_BLL;
using V5_Model;
using V5_Common;
using System.Collections.Generic;

namespace V5_EShop.Admin.Product {
    public partial class Admin_ProductClass : BasicPage {
        protected BLLProductClass bllProductClass = new BLLProductClass();
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string act = InputHelper.SetString(Request["action"]);
                Bind_DropClassList();
                Bind_ProductFieldPropery();
                switch (act) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_DataList();
                        break;
                }
            }
        }

        private void Bind_ProductFieldPropery() {
            DataTable dt = new BLLProductFieldClass().GetList("").Tables[0];
            this.ddlProductFieldProperty.DataSource = dt;
            this.ddlProductFieldProperty.DataTextField = "FieldClassName";
            this.ddlProductFieldProperty.DataValueField = "ID";
            this.ddlProductFieldProperty.DataBind();
            this.ddlProductFieldProperty.Items.Insert(0, new ListItem("请选择", "0"));
        }


        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            DataTable listModel = bllProductClass.GetList(" Order by `ClassCode`").Tables[0];
            this.gvList.DataSource = listModel;
            this.gvList.DataBind();
        }

        private void Bind_DropClassList() {

            BLLProductClass bllSiteLinkClass = new BLLProductClass();
            DataTable dtClassList = bllSiteLinkClass.GetDropDownList(0, "作为一级栏目");
            this.ddlClassID.DataSource = dtClassList;
            this.ddlClassID.DataTextField = "ClassName";
            this.ddlClassID.DataValueField = "ClassID";
            this.ddlClassID.DataBind();

        }

        private void Bind_DataEdit(int ClassID) {
            this.MultiViewList.ActiveViewIndex = 1;
            this.lblTitle.Text = "修改栏目";
            this.btnSave.Text = "保存修改";
            DataTable dt = bllProductClass.GetList(" And ClassID=" + ClassID).Tables[0];
            this.hfClassID.Value = ClassID.ToString();
            this.txtClassName.Text = dt.Rows[0]["ClassName"].ToString();
            this.ddlClassID.SelectedValue = dt.Rows[0]["ParentID"].ToString();
            this.txtInfo.Text = dt.Rows[0]["Info"].ToString();
        }

        private void Bind_DataDelete(int ClassID) {
            bllProductClass.Delete(ClassID);
            this.Bind_DataList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int hfClassID = InputHelper.SetNumber(this.hfClassID.Value);
            string ClassName = this.txtClassName.Text;
            int ParentID = int.Parse("0" + this.ddlClassID.SelectedValue);
            string Info = this.txtInfo.Text;

            ModelProductClass model = new ModelProductClass();
            model.ClassID = hfClassID;
            model.ClassName = ClassName;
            model.ParentID = ParentID;
            model.Info = Info;

            if (hfClassID == 0) {
                hfClassID = bllProductClass.GetMaxId();
                model.ClassID = hfClassID;
                bllProductClass.Add(model);
            }
            else if (hfClassID > 0) {
                bllProductClass.Update(model);
            }
            Response.Redirect("Admin_ProductClass.aspx");
            //this.Bind_DataList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int ClassID = InputHelper.SetNumber(e.CommandArgument);
            this.Bind_DropClassList();
            switch (eName) {
                case "go":
                    Bind_DataEdit(ClassID);
                    break;
                case "del":
                    Bind_DataDelete(ClassID);
                    break;
                case "addsubclass":
                    this.MultiViewList.ActiveViewIndex = 1;
                    this.ddlClassID.SelectedValue = ClassID.ToString();
                    break;
                case "up":
                    bllProductClass.MoveClass(ClassID, 1);
                    this.Bind_DataList();
                    break;
                case "down":
                    bllProductClass.MoveClass(ClassID, -1);
                    this.Bind_DataList();
                    break;
            }
        }
    }
}